package com.fw.service.applicationlog.impl;

import org.apache.log4j.Logger;
import org.springframework.beans.BeanUtils;

import com.fw.service.applicationlog.ApplicationLogService;
import com.fw.service.applicationlog.bean.ApplicationLogBean;

public class ApplicationLogServiceImpl implements ApplicationLogService {

    private static Logger log = Logger.getLogger(ApplicationLogService.class.getName());

    @Override
    public ApplicationLogBean createApplicationLog(ApplicationLogBean applicationLogBean) {
	ApplicationLogBean resultBean = null;
	if (applicationLogBean != null) {
	    String type = applicationLogBean.getType();
	    if (type != null
		    && (ApplicationLogBean.TYPE_INFO.equals(type) || ApplicationLogBean.TYPE_WARN.equals(type) || ApplicationLogBean.TYPE_ERROR
			    .equals(type))) {
		String message = applicationLogBean.getMessage();
		String clazz = applicationLogBean.getClazz();
		String method = applicationLogBean.getMethod();
		StringBuffer messageBuffer = new StringBuffer();
		if (clazz != null && !clazz.isEmpty()) {
		    messageBuffer.append(clazz);
		    messageBuffer.append(", ");
		}
		if (method != null && !method.isEmpty()) {
		    messageBuffer.append(method);
		    messageBuffer.append(", ");
		}
		if (message != null && !message.isEmpty()) {
		    messageBuffer.append(message);
		}
		if (ApplicationLogBean.TYPE_INFO.equals(type)) {
		    log.info("info : " + messageBuffer.toString());
		} else if (ApplicationLogBean.TYPE_WARN.equals(type)) {
		    log.warn("warn : " + messageBuffer.toString());
		} else if (ApplicationLogBean.TYPE_ERROR.equals(type)) {
		    log.error("error : " + messageBuffer.toString());
		}
		resultBean = new ApplicationLogBean();
		BeanUtils.copyProperties(applicationLogBean, resultBean);
	    }
	}
	return resultBean;
    }
}
